package com.mzy.linear_struct.subject;

import java.util.Arrays;

/**
 * @Author: codermzy
 * @Date: 2024/04/24/下午6:50
 * @Description:
 */
public class 轮转数组189 {
    static class Solution {
        public void rotate(int[] nums, int k) {
            for (int i = 0; i < k; i++) {
                int num = nums[nums.length - 1];
                for (int j = nums.length - 1; j > 0; j--) {
                    nums[j] = nums[j - 1];
                }
                nums[0] = num;
            }
        }


        public void rotate1(int[] nums, int k) {
            int right = nums.length - 1;
            for (int i = k - 1; i >= 0; i--) {
                swap(nums, i, right);
                right--;
            }
        }

        private void swap(int[] nums, int left, int right) {
            int temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
        }
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7};
        new Solution().rotate1(arr, 2);

        System.out.println(Arrays.toString(arr));
    }
}
